#pragma once
#include "stdafx.h"
#include <iostream>

template<class T>
class MyArray
{
	T* list;
	int currSize;
	int maxSize;

	void resize() {		
		maxSize = maxSize * 2;
		T* tempList = new T[maxSize];
		for(int i=0;i<currSize;i++) {
			tempList[i] = list[i];
		}
		delete[] list;
		list = tempList;

	};

	void moveAllLeftFrom(int index) {
		for(int i=index; i< currSize-1; i++) {
			list[i] = list[i+1];
		}
	};

	void moveAllRightFrom(int index) {
		for(int i=currSize+1; i> index; i--) {
			list[i] = list[i-1];
		}
	};
public:
	MyArray() { list = new T[10]; currSize = 0; maxSize = 10;};
	~MyArray() { delete[] list; };
	
	bool add(int index, T object) {
		if(currSize == maxSize) {
			resize();
		}
		if(index < currSize && index > -1) {
			moveAllRightFrom(index);
			list[index] = object;
			currSize++;
			return true;
		}
		return false;
	};

	bool add(T object) { 
		if(currSize == maxSize) {
			resize();
		}
		list[currSize] = object; 
		currSize ++; 
		return true;
	} ;

	void clear() {
		delete[] list;
		list = new T[10];
		currSize = 0;
		maxSize = 10;
	};

	bool contains(T object) {
		for(int i=0;i<currSize;i++) {
			if(list[i] == object) {
				return true;
			}
		}
		return false;
	};

	T get(int index) {
		if(index < currSize && index > -1) {
			return list[index];
		}
		return NULL;
	};

	int indexOf(T object) {
		for(int i=0;i<currSize;i++) {
			if(list[i] == object) {
				return i;
			}
		}	
		return -1;
	};

	bool isEmpty() {
		if(currSize == 0) {
			return true;
		}
		return false;
	};

	T remove(T object) {
		for(int i=0; i<currSize; i++) {
			if(list[i] == object) {
				T temp = list[i];
				moveAllLeftFrom(i);
				currSize--;
				return temp;
			}
		}
		return NULL;
	};


	T set(int index, T object) {
		if(index < currSize && index > -1) {
			T temp = list[index];
			list[index] = object;
			return temp;
		}
		return NULL;
	}

	int size() {
		return currSize;
	}

	void dump() { 
		for(int i=0; i<currSize; i++) { 
			std::cout << "[" << i << "] = " << list[i] << std::endl;
		}
	};
};

